package com.mars.admin.modules.banner.service.impl;


import com.mars.admin.modules.banner.entity.Banner;
import com.mars.admin.modules.banner.mapper.BannerMapper;
import com.mars.admin.modules.banner.service.BannerService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public void add(Banner banner) {
        bannerMapper.insert(banner);
    }

    @Override
    public void updateCategory(Banner banner) {
        bannerMapper.update(banner);
    }

    @Override
    public void delete(Long id) {
        bannerMapper.deleteById(id);
    }

    @Override
    public List<Banner> listAll(String type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        // 状态为启用
        queryWrapper.eq(Banner::getStatus, 1)
                // 未删除
                .eq(Banner::getIsDeleted, 0)
                .eq(Banner::getType, type)
                // 按排序值降序排列（值越小越靠前应该是升序，这里根据需求调整）
                .orderBy(Banner::getSort,false);


        return list(queryWrapper);
    }

    @Override
    public Page<Banner> pageBanner(Page<Banner> page, String title, String type, String position, Integer status) {
        QueryWrapper queryWrapper = new QueryWrapper();
        
        if (StringUtils.hasText(title)) {
            queryWrapper.like(Banner::getTitle, title);
        }
        if (StringUtils.hasText(type)) {
            queryWrapper.eq(Banner::getType, type);
        }
        if (StringUtils.hasText(position)) {
            queryWrapper.eq(Banner::getPosition, position);
        }
        if (status != null) {
            queryWrapper.eq(Banner::getStatus, status);
        }
        
        queryWrapper.eq(Banner::getIsDeleted, 0)
                   .orderBy(Banner::getSort, true)
                   .orderBy(Banner::getCreateTime, false);
        
        return page(page, queryWrapper);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        if (ids != null && !ids.isEmpty()) {
            bannerMapper.deleteBatchByIds(ids);
        }
    }
}
